﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the Microsoft Public License (Ms-PL).
// See accompanying file License.txt or copy at http://mews.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Mews.Application
{
    using System;
    using System.IO;
    using System.Windows.Forms;

    using Mews.IO;

    /// <summary>The form used for the editing of an outbound article.</summary>
    internal sealed partial class EditArticleForm : Form
    {
        internal EditArticleForm(
            Form owner, bool readOnly, string groups, string subject, int subjectMaxLength, string body)
        {
            this.InitializeComponent();
            this.Owner = owner;
            this.postMenuItem.Enabled = !readOnly;
            this.saveToDraftsMenuItem.Enabled = !readOnly;
            this.groupsTextBox.ReadOnly = readOnly;
            this.subjectTextBox.ReadOnly = readOnly;
            this.bodyTextBox.ReadOnly = readOnly;
            this.groupsTextBox.Text = groups;
            this.subjectTextBox.Text = subject;
            this.subjectTextBox.MaxLength = subjectMaxLength;

            using (var stream = new MemoryStream(body == null ? 512 : body.Length))
            {
                using (var forwardingStream = new ForwardingStream(stream))
                using (var flowedStream = new FlowedStream(forwardingStream, false, true, 40))
                using (var writer = new StreamWriter(flowedStream))
                {
                    writer.Write(body);
                }

                stream.Seek(0, SeekOrigin.Begin);

                using (var reader = new StreamReader(stream))
                {
                    this.bodyTextBox.Text = reader.ReadToEnd();
                }
            }

            if (string.IsNullOrEmpty(subject))
            {
                this.subjectTextBox.Focus();
            }
            else
            {
                this.bodyTextBox.Focus();
            }
        }

        internal string Groups
        {
            get { return this.groupsTextBox.Text; }
        }

        internal string Subject
        {
            get { return this.subjectTextBox.Text; }
        }

        internal string Body
        {
            get
            {
                using (var stream = new MemoryStream(this.bodyTextBox.Text.Length))
                {
                    using (var forwardingStream = new ForwardingStream(stream))
                    using (var writer = new StreamWriter(forwardingStream))
                    {
                        writer.Write(this.bodyTextBox.Text);
                    }

                    stream.Seek(0, SeekOrigin.Begin);

                    using (var flowedStream = new FlowedStream(stream, false, true, 40))
                    using (var reader = new StreamReader(flowedStream))
                    {
                        return reader.ReadToEnd();
                    }
                }
            }
        }

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        private void OnPostMenuItem_Click(object sender, EventArgs e)
        {
            this.Close(DialogResult.Yes);
        }

        private void OnSaveToDraftsMenuItem_Click(object sender, EventArgs e)
        {
            this.Close(DialogResult.No);
        }

        private void OnCancelArticleMenuItem_Click(object sender, EventArgs e)
        {
            this.Close(DialogResult.Cancel);
        }

        private void OnEditArticleForm_Closed(object sender, EventArgs e)
        {
            // The result can only be OK if the user has left the dialog with the back button, tapped ok or the
            // application is being stopped through the task manager
            if (this.DialogResult == DialogResult.OK)
            {
                // Since the user has not said clearly whether to post, save to drafts or cancel changes, it seems
                // most natural to save to drafts only if anything has changed and leave the state as is otherwise.
                this.DialogResult =
                    this.groupsTextBox.Modified || this.bodyTextBox.Modified || this.subjectTextBox.Modified ?
                    DialogResult.No : DialogResult.Cancel;
            }
        }

        private void Close(DialogResult dialogResult)
        {
            this.DialogResult = dialogResult;
            this.Close();
        }
    }
}